package com.videoadmin.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.videoadmin.dao.IMoviesDAO;
import com.videoadmin.pojo.Movies;
import com.videoadmin.utils.HibernateUtil;

public class MoviesDAOImpl implements IMoviesDAO {

	/* (non-Javadoc)
	 * @see com.video.dao.IMoviesDao#saveOrUpdate(com.domain.Movies)
	 */
	public void saveOrUpdate(Movies movie){
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.saveOrUpdate(movie);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.IMoviesDao#delete(com.domain.Movies)
	 */
	public void delete(Movies movie){
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.delete(movie);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.IMoviesDao#findById(int)
	 */
	public Movies findById(int id){
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			Movies movie = (Movies) s.get(Movies.class, id);
			return movie;
		} finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.IMoviesDao#findByName(java.lang.String)
	 */
	public List<Movies> findByName(String keyword){
		Session s=null;
		List<Movies> list=new ArrayList<Movies>();
		try{
			s = HibernateUtil.getSession();
			list=(List<Movies>)s.createSQLQuery("select * from Movies where MovieName like '%"+keyword+"%'").addEntity(Movies.class).list();
			return list;
		}finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.IMoviesDao#findByDirector(java.lang.String)
	 */
	public List<Movies> findByDirector(String keyword){
		Session s=null;
		List<Movies> list=new ArrayList<Movies>();
		try{
			s = HibernateUtil.getSession();
			list=(List<Movies>)s.createSQLQuery("select * from Movies where Director='"+keyword+"'").addEntity(Movies.class).list();
			return list;
		}finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.IMoviesDao#findByActor(java.lang.String)
	 */
	public List<Movies> findByActor(String keyword){
		Session s=null;
		List<Movies> list=new ArrayList<Movies>();
		try{
			s = HibernateUtil.getSession();
			list=(List<Movies>)s.createSQLQuery("select * from Movies where Actor='"+keyword+"'").addEntity(Movies.class).list();
			return list;
		}finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.IMoviesDao#findByAll(java.lang.String)
	 */
	public List<Movies> findByAll(String keyword){
		Session s=null;
		List<Movies> list=new ArrayList<Movies>();
		try{
			s = HibernateUtil.getSession();
			list=(List<Movies>)s.createSQLQuery("select * from Movies where MovieName='"+
        			keyword+"' or Director='"+keyword+"' or Actor='"+keyword+"'").addEntity(Movies.class).list();
			return list;
		}finally {
			if (s != null)
				s.close();
		}
	}

}
